<?php

declare(strict_types=1);

namespace app\controller\system\permission;

use app\BaseController;
use app\service\system\RoleService;
use owns\exception\TransException;

class Role extends BaseController
{
    protected $roleService;

    public function initialize()
    {
        $this->roleService = make(RoleService::class);
    }
    /**
     * 角色列表
     */
    public function list()
    {
        return $this->success($this->roleService->list($this->paging()));
    }
    /**
     * 角色编辑
     */
    public function edit()
    {
        $data = $this->request->post();
        $this->validate($data, [
            'id' => 'integer|egt:0',
            'name' => 'require|length:2,20',
            'alias' => 'require|length:2,20',
            'status' => 'integer|in:0,1',
            'sort' => 'integer|egt:1|elt:255',
        ]);
        return TransException::exec(RoleService::class, 'edit', $data, (int) $data['id']);
    }
    /**
     * 角色权限
     */
    public function permission()
    {
        $data = $this->request->post();
        $this->validate($data, [
            'id' => 'integer|egt:0',
            'data_scope' => 'integer|in:1,2,3,4',
        ]);
        return TransException::exec(RoleService::class, 'permission', $data, (int) $data['id']);
    }
    /**
     * 角色权限菜单id
     */
    public function roleMenu()
    {
        $param = $this->request->get();
        $res['menu'] = RoleService::roleMenuId((int) $param['id']);
        return $this->success($res);
    }
}
